package com.powerbank.device.controller;

import com.powerbank.device.dto.DeviceAlertDTO;
import com.powerbank.device.service.DeviceMonitorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备监控API
 */
@Slf4j
@RestController
@RequestMapping("/monitor")
public class DeviceMonitorController {

    @Autowired
    private DeviceMonitorService deviceMonitorService;

    /**
     * 检查设备健康状态
     */
    @GetMapping("/health/{deviceCode}")
    public boolean checkDeviceHealth(@PathVariable String deviceCode) {
        return deviceMonitorService.checkDeviceHealth(deviceCode);
    }

    /**
     * 获取设备报警列表
     */
    @GetMapping("/alerts/{deviceCode}")
    public List<DeviceAlertDTO> getDeviceAlerts(@PathVariable String deviceCode,
                                                @RequestParam(defaultValue = "20") int limit) {
        return deviceMonitorService.getDeviceAlerts(deviceCode, limit);
    }

    /**
     * 获取所有活跃报警
     */
    @GetMapping("/alerts/active")
    public List<DeviceAlertDTO> getActiveAlerts() {
        return deviceMonitorService.getActiveAlerts();
    }

    /**
     * 手动创建报警（测试用）
     */
    @PostMapping("/alerts")
    public String createAlert(@RequestBody DeviceAlertDTO alert) {
        deviceMonitorService.createAlert(alert);
        return "报警创建成功";
    }

    /**
     * 解除报警
     */
    @PostMapping("/alerts/resolve/{alertId}")
    public String resolveAlert(@PathVariable String alertId) {
        boolean success = deviceMonitorService.resolveAlert(alertId);
        return success ? "报警解除成功" : "报警解除失败";
    }

    /**
     * 批量解除报警
     */
    @PostMapping("/alerts/batch-resolve")
    public String batchResolveAlerts(@RequestBody List<String> alertIds) {
        int successCount = deviceMonitorService.batchResolveAlerts(alertIds);
        return String.format("批量解除报警完成，成功解除 %d/%d 条报警", successCount, alertIds.size());
    }

    /**
     * 模拟设备离线报警
     */
    @PostMapping("/test/offline/{deviceCode}")
    public String testOfflineAlert(@PathVariable String deviceCode) {
        deviceMonitorService.handleDeviceOfflineAlert(deviceCode);
        return "离线报警已创建";
    }

    /**
     * 模拟设备故障报警
     */
    @PostMapping("/test/error/{deviceCode}")
    public String testErrorAlert(@PathVariable String deviceCode,
                                @RequestParam String errorCode,
                                @RequestParam String errorMessage) {
        deviceMonitorService.handleDeviceErrorAlert(deviceCode, errorCode, errorMessage);
        return "故障报警已创建";
    }

    /**
     * 模拟电量低报警
     */
    @PostMapping("/test/low-battery/{deviceCode}")
    public String testLowBatteryAlert(@PathVariable String deviceCode,
                                     @RequestParam Integer batteryLevel) {
        deviceMonitorService.handleLowBatteryAlert(deviceCode, batteryLevel);
        return "电量低报警已创建";
    }

    /**
     * 模拟插槽故障报警
     */
    @PostMapping("/test/slot-error/{deviceCode}/{slotNo}")
    public String testSlotErrorAlert(@PathVariable String deviceCode,
                                    @PathVariable Integer slotNo,
                                    @RequestParam String errorCode,
                                    @RequestParam String errorMessage) {
        deviceMonitorService.handleSlotErrorAlert(deviceCode, slotNo, errorCode, errorMessage);
        return "插槽故障报警已创建";
    }
}